#!/usr/bin/env bash
ETALON=$1
ASSESSED=$2

paste "$ETALON" "$ASSESSED" |
    awk -F '	' '
BEGIN {
    true_positive   =   0;
    true_negative   =   0;
    false_positive  =   0;
    false_negative  =   0;
}
{
    if (($1 == $2) && ($1 == 1)) {
        true_positive   +=  1;
    }

    if (($1 == $2) && ($1 == -1)) {
        true_negative   +=  1;
    }

    if (($1 != $2) && ($1 == -1)) {
        false_positive  +=  1;
    }

    if (($1 != $2) && ($1 == 1)) {
        false_negative  +=  1;
    }
}

END {
    if ((false_positive == 0) && (true_positive == 0)) {
        precision   =   0;
    } else {
        precision   =   true_positive / (false_positive + true_positive);
    }

    if ((false_negative == 0) && (true_positive == 0)) {
        recall      =   0;
    } else {
        recall      =   true_positive / (true_positive + false_negative);
    }

    if ((precision == 0) && (recall == 0)) {
        f1_measure  =   0;
    } else {
        f1_measure  =   2 * precision * recall / (precision + recall);
    }
    print precision "	" recall "	" f1_measure

}'

